/**
 * 案件受理脚本
 */
const ajax = require('../../tool/ajax');
const login = require('../login/index');
const preview = require('../component/signaturePreview');//签章预览
const affirm = require('../component/receiverAffirm');//确认收件人
const caseDetail = require('../component/caseDetail');//案件详情右侧按钮
const screen = require('../component/screen');//截图
let caseFile = require('../component/caseFile');//读写案件信息
let pupConfig, page, config, filing, caseInfo;

/**
 * 开始任务
 * @param myPupConfig 框架配置信息
 * @param myCaseInfo 案件信息：{caseId:'案件ID', secretaryId: '秘书ID'}
 * @returns {Promise<void>}
 */
const start = async (myPupConfig, myCaseInfo) => {
  pupConfig = myPupConfig;
  page = pupConfig.page;
  config = pupConfig.config.common;
  filing = pupConfig.config.filing;
  caseInfo = myCaseInfo;

  await step1();
  await step2();
  await step3();
}

/**
 * 步骤一：受理案件
 */
const step1 = async () => {
  await login(pupConfig, caseInfo.secretaryId);//秘书登录
  await inputAcceptInfo();
  await notificationMake(0);
  await affirm(pupConfig, 0);
}

/**
 * 步骤二：上传缴费单
 */
const step2 = async () => {
  await login(pupConfig);//申请人登录
  await uploadBill();
}

/**
 * 步骤三：确认缴费单
 */
const step3 = async () => {
  await login(pupConfig, caseInfo.secretaryId);//秘书登录
  await checkPaymentSlip();
  await notificationMake(1);
  await affirm(pupConfig, 1);
}

/**
 * 输入案件受理信息
 */
const inputAcceptInfo = async () => {
  //打开案件受理弹框
  await caseDetail(pupConfig, caseInfo.caseId, 2);

  await ajax.waitForResponse(page, 'queryCaseSummaryList.do');

  await page.waitFor('.line-right textarea');
  await page.type('.line-right textarea', '秘书意见');

  await screen(page, '案件受理', 1);

  await page.click('.ant-modal-footer button');
}

/**
 * 通知书制作
 * @param type 通知书类型，0代表案件受理，1代表确认缴费，默认为0
 */
const notificationMake = async (type = 0) => {
  /**
   * 点击制作通知书
   * @param index 通知书序号
   */
  let make = async (index) => {
    await ajax.waitForResponse(page, 'queryNoticeFileList.do');
    let selector = type === 0 ? 'td a' : 'td span.table-operation';
    let as = await page.$$(selector);
    await as[index].click();
    //保存案件识别码
    if (type === 1 && index === 0) {
      await saveAffinityCode();
    }
    await preview(page);
  }

  if (type === 0) {
    await make(0);
    await make(1);
  } else if (type === 1) {
    await make(0);
    await make(4);
  }

  await ajax.waitForResponse(page, 'queryNoticeFileList.do');

  await screen(page, '通知书制作' + type, 1);

  await page.click('.table-submit button');
}

/**
 * 保存案件识别码
 */
const saveAffinityCode = async () => {
  let result = await ajax.waitForResponse(page, 'createArbitrationNotice.do');
  caseInfo.affinityCode = result.data.againstCaseRecognitionCode;
  await caseFile.write(caseInfo);//保存案件信息
}

/**
 * 上传缴费回单
 */
const uploadBill = async () => {
  //点击上传缴费回单按钮
  await caseDetail(pupConfig, caseInfo.caseId, 1);

  /**
   * 上传缴费回单
   */
  await page.waitFor('.ant-modal-body span.ant-upload input');
  let input = await page.$('.ant-modal-body span.ant-upload input');
  await input.uploadFile(config.imgPathUpload + 'girl.png');
  await ajax.waitForResponse(page, 'updFileStatus.do');

  await screen(page, '上传缴费回单', 1);

  /**
   * 点击确认上传
   */
  await page.click('.ant-modal-footer button.tool-modal-confirm')
  await ajax.waitForResponse(page, 'uploadPaymentReceipt.do');
}

/**
 * 确认缴费回单
 */
const checkPaymentSlip = async () => {
  //点击确认缴费回单按钮
  await caseDetail(pupConfig, caseInfo.caseId, 0);

  await ajax.waitForResponse(page, 'getFileInfo.do');

  await screen(page, '确认缴费回单', 1);

  /**
   * 点击确认
   */
  await page.click('.ant-modal-footer button.ant-btn-primary')
  await ajax.waitForResponse(page, 'surePayment.do');
}

module.exports = start;